package com.ld.collegetask;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.MenuItem;

import com.ld.collegetask.fragment.DynamicFragment;
import com.ld.collegetask.fragment.MemberFragment;
import com.ld.collegetask.fragment.MessageFragment;
import com.ld.collegetask.fragment.OrderFragment;
import com.ld.collegetask.login.LoginActivity;
import com.ld.collegetask.utils.SharePreference;

import java.lang.reflect.Field;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.api.BasicCallback;

public class MainActivity extends BaseActivity {

    private DynamicFragment mDynamicFragment;
    private MemberFragment mMemberFragment;
    private MessageFragment mMessageFragment;
    private OrderFragment mOrderFragment;
    private SharePreference sp;
    private BottomNavigationView navigation;

    private Fragment[] fragments;
    private int lastShowFragment = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navigation = (BottomNavigationView) findViewById(R.id.navigation);
        disableShiftMode(navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigation.setItemIconTintList(null);
        initFragments();
        setT(0);

        sp = new SharePreference(getBaseContext());
        if (sp.getLoginState()){
            JMessageClient.login(sp.getImId(), sp.getImPassword(), new BasicCallback() {
                @Override
                public void gotResult(int i, String s) {
                    Log.i("im",i+"*****"+s);
                }
            });
        }
//        sp.setUserId(22);
    }

    @Override
    public void loading() {

    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_order:
                    setT(0);
                    if (lastShowFragment != 0) {
                        switchFrament(lastShowFragment, 0);
                        lastShowFragment = 0;
                    }
                    return true;
                case R.id.navigation_dynamic:
                    setT(1);
                    if (lastShowFragment != 1) {
                        switchFrament(lastShowFragment, 1);
                        lastShowFragment = 1;
                    }
                    return true;
                case R.id.navigation_message:
                    setT(2);
                    if (lastShowFragment != 2) {
                        if (sp.getLoginState()) {
                        switchFrament(lastShowFragment, 2);
                        lastShowFragment = 2;
                        }else {
                            setT(lastShowFragment);
                            startActivity(new Intent(MainActivity.this, LoginActivity.class));
                        }
                    }
                    return true;
                case R.id.navigation_member:
                    setT(3);
                    if (lastShowFragment != 3) {
                        if (sp.getLoginState()) {
                            switchFrament(lastShowFragment, 3);
                            lastShowFragment = 3;
                        }else {
                            setT(lastShowFragment);
                            startActivity(new Intent(MainActivity.this, LoginActivity.class));
                        }
                    }
                    return true;
            }
            return false;
        }

    };

    private void setT(int i){
        for (int j = 0;j < 4;j++){
            navigation.getMenu().getItem(j).setEnabled(true);
            if (i == j){
                navigation.getMenu().getItem(j).setEnabled(false);
            }
        }
    }

    /**
     * 反射关闭动画，去掉了动画效果不一样
     *
     * @param view
     */
    public static void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                item.setShiftingMode(false);
                item.setChecked(false);
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 切换Fragment
     *
     * @param lastIndex 上个显示Fragment的索引
     * @param index     需要显示的Fragment的索引
     */
    public void switchFrament(int lastIndex, int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments[lastIndex]);
        if (!fragments[index].isAdded()) {
            transaction.add(R.id.content, fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }

    private void initFragments() {
        mOrderFragment = OrderFragment.getInstance();//抢单
        mDynamicFragment = DynamicFragment.getInstance();//说说
        mMessageFragment = MessageFragment.getInstance();//消息
        mMemberFragment = MemberFragment.getInstance();//我的

        fragments = new Fragment[]{mOrderFragment, mDynamicFragment, mMessageFragment, mMemberFragment};
        lastShowFragment = 0;
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.content, mOrderFragment)
                .show(mOrderFragment)
                .commit();
    }

}
